package com.liu.juc.collection;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Map的线程不安全
 */
public class UnsafeMapDemo {

    /**
     * hashMap 线程不安全 :ConcurrentModificationException 并发修改异常
     * 方案一：ConcurrentHashMap
     * 方案二：Collections.synchronizedMap(new HashMap<>(10));
     * @param args
     */
    public static void main(String[] args) {
        //Map<String, String> map = new ConcurrentHashMap<>(10);
        Map<String, String> map  = Collections.synchronizedMap(new HashMap<>(10));
        for (int i = 1; i <= 100; i++) {
            new Thread(() -> {
                map.put(UUID.randomUUID().toString().substring(0, 5), UUID.randomUUID().toString().substring(0, 5));
                System.out.println(map);
            }, String.valueOf(i)).start();

        }

    }
}
